package datastructure.array;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author XY
 * @version 1.0.0
 * @ClassName LC904.java
 * @createTime 2025年07月28日 19:15:00
 */
public class LC904 {
    public int totalFruit(int[] fruits) {
        //滑动窗口左边界
        int left =0;
        int count = 0;
        int duplicateCount = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < fruits.length; i++) {
            if (map.get(fruits[i]) == null) {
                if (map.size() >= 2) {
                    left = i - duplicateCount;
                    map.remove(fruits[left - 1]);
                }
                map.put(fruits[i], i);
            }
            if (i > 0 && fruits[i] != fruits[i - 1]) {
                duplicateCount = 0;
            }
            duplicateCount++;
            count = Math.max(count, i - left + 1);
        }
        return count;
    }
}
